define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'develop/acuisitionscheme/index' + location.search,
                    add_url: 'develop/acuisitionscheme/add',
                    add_detail_url: 'develop/acuisitionscheme/add_detail',
                    edit_url: 'develop/acuisitionscheme/edit',
                    del_url: 'develop/acuisitionscheme/del',
                    multi_url: 'develop/acuisitionscheme/multi',
                    import_url: 'develop/acuisitionscheme/import',
                    table: 'acquisition_scheme',
                },
                pageSize: 15,
                pageList: [15],
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                dblClickToEdit: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                        {field: 'detail.trademark_printing', title: __('Detail.trademark_printing'), operate: 'LIKE'},
                        {field: 'detail.abbreviation', title: __('标识'), operate: 'LIKE'},
                        {field: 'colour.name', title: __('Colour.name'), operate: 'LIKE'},
                        {field: 'detail.quantity', title: __('Detail.quantity')},
                        {field: 'detail.cumulative_quantity', title: __('Detail.cumulative_quantity')},
                        {field: 'unallocated_quantity', title: __('未分配数量'),formatter: function (value, row, index) {
                                return row.detail.quantity - row.detail.cumulative_quantity;
                            }},

                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('退料')}, formatter: Table.api.formatter.status},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'adddetail',
                                    text: __('领料分配'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'develop/acuisitionscheme/add_detail'
                                },
                                {
                                    name: 'showdetail',
                                    text: __('分配信息'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'develop/acuisitionscheme/show_detail?'
                                }
                            ],formatter: Table.api.formatter.operate}

                        // <a href="javascript:;"
                        //    className="btn btn-success btn-add {:$auth->check('statistics/acquisitionschemedetail/add')?'':'hide'}"
                        //    title="{:__('Add')}"><i className="fa fa-plus"></i> {:__('Add')}</a>
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'develop/acuisitionscheme/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'develop/acuisitionscheme/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'develop/acuisitionscheme/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();

        },
        add_detail: function () {
            Controller.api.bindevent();
            $(document).on("input","#c-quantity",function (){   //更换订单id的时候清除其他菜单
                var val = $("#no-cumulative").val();
                var nowValue = $(this).val();
                if(val==0){
                    layer.open({
                        content: "不允许分配：该订单已完成分配",
                        title:"警告"

                    })
                }else if(Number(nowValue)>Number(val)){
                    $(this).val('')
                    layer.open({
                        content: '数量不能超过'+val
                    });
                }

            });

        },
        show_detail: function () {
            var val=$("input[name='row[acquisition_scheme_id]']").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'statistics/acquisitionschemedetail/index' + location.search,
                    add_url: 'develop/acuisitionscheme/add_detail?ids='+val,
                    edit_url: 'statistics/acquisitionschemedetail/edit',
                    // del_url: 'statistics/acquisitionschemedetail/del',
                    multi_url: 'statistics/acquisitionschemedetail/multi',
                    import_url: 'statistics/acquisitionschemedetail/import',
                    table: 'acquisition_scheme_detail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                dblClickToEdit: false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'quantity', title: __('分配数量')},
                        {field: 'employee.name', title: __('Employee.name'), operate: 'LIKE'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":'待发料',"1":'已发料',"2":'退料'}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:Table.api.formatter.operate
                            ,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: '编辑',
                                    title: '编辑',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success btn-editone btn-dialog',
                                    url: 'options.extend.edit_url',
                                    disable: function (row) {
                                        console.log(row,'ff')
                                        if(row.status=='1'){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
